package com.xunwulian.weixin.h5.config;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(WxProperty.class)
public class WxMpConfig {

    @Autowired
    private WxProperty wxProperty;

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
        configStorage.setAppId(wxProperty.getAppid());
        configStorage.setSecret(wxProperty.getAppsecret());
        configStorage.setToken(wxProperty.getToken());
        configStorage.setAesKey(wxProperty.getAesKey());
        configStorage.setPartnerId(wxProperty.getPartenerId());
        configStorage.setPartnerKey(wxProperty.getPartenerKey());
        return configStorage;
    }

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

}
